ggplot2 tutorial

Sebastian Campbell

April 12, 2017

What is ggplot2?

ggplot2

  • ggplot2 is a graphical system to make plots using the ‘Grammar of Graphics’
  • Helpful for thinking about plots

Grammar of graphics

  • A way to think about graphics
  • Graphics are mapping of data to aesthetic attributes of geometric objects
  • May also contain transformations and facetting

ggplot2 itself

  • Developed by Hadley Wickham and has been out since 2005
  • Mainly focused on static graphics
  • ggvis is for interactive graphs - I don’t recommend it
  • plotly is better

Making a basic plot

Load some data

library(ggplot2)
mpg
## # A tibble: 234 × 11
##    manufacturer      model displ  year   cyl      trans   drv   cty   hwy
##           <chr>      <chr> <dbl> <int> <int>      <chr> <chr> <int> <int>
## 1          audi         a4   1.8  1999     4   auto(l5)     f    18    29
## 2          audi         a4   1.8  1999     4 manual(m5)     f    21    29
## 3          audi         a4   2.0  2008     4 manual(m6)     f    20    31
## 4          audi         a4   2.0  2008     4   auto(av)     f    21    30
## 5          audi         a4   2.8  1999     6   auto(l5)     f    16    26
## 6          audi         a4   2.8  1999     6 manual(m5)     f    18    26
## 7          audi         a4   3.1  2008     6   auto(av)     f    18    27
## 8          audi a4 quattro   1.8  1999     4 manual(m5)     4    18    26
## 9          audi a4 quattro   1.8  1999     4   auto(l5)     4    16    25
## 10         audi a4 quattro   2.0  2008     4 manual(m6)     4    20    28
## # ... with 224 more rows, and 2 more variables: fl <chr>, class <chr>

Basic scatter plot

ggplot(mpg, aes(x = cty, y = hwy)) +
  geom_point()

Colour by cylinders

ggplot(mpg, aes(x = cty, y = hwy, colour = cyl)) +
  geom_point()

Cylinders as class

ggplot(mpg, aes(x = cty, y = hwy, colour = as.factor(cyl))) +
  geom_point()

Change colour scale

ggplot(mpg, aes(x = cty, y = hwy, colour = as.factor(cyl))) +
  geom_point() + scale_color_brewer(palette = "YlOrRd") + theme_dark()

Size by cylinders

ggplot(mpg, aes(x = cty, y = hwy, size = cyl)) +
  geom_point()

Shape by cylinders

ggplot(mpg, aes(x = cty, y = hwy, shape = as.factor(cyl))) +
  geom_point(size = 3)

Facet by cylinders

ggplot(mpg, aes(x = cty, y = hwy)) +
  geom_point() + facet_grid(.~cyl)

Other transformations

Adding stats

ggplot(mpg, aes(x = cty, y = hwy)) +
  geom_point() + geom_smooth(method = "lm")

Changing coordinates - diamonds

ggplot(diamonds, aes(x = carat, y = price)) +
  geom_point()

Log transform

ggplot(diamonds, aes(x = carat, y = price)) +
  geom_point() + coord_trans(x = "log10", y = "log10")

Other kinds of plots

Bar charts

ggplot(mpg, aes(x = as.factor(cyl), y = hwy)) + 
  geom_bar(stat = "summary", fun.y = "mean")

Bar charts with polar coordinates

ggplot(mpg, aes(x = factor(1), y = hwy, fill = as.factor(cyl))) + 
  geom_bar(stat = "summary", fun.y = "mean", width = 1) + coord_polar(theta = "y")

Boxplots

ggplot(mpg, aes(x = as.factor(cyl), y = hwy)) + 
  geom_boxplot()

Violin plots

ggplot(mpg, aes(x = as.factor(cyl), y = hwy)) + 
  geom_violin()

Heatmaps

ggplot(diamonds, aes(x = carat, price))+ 
  geom_hex()

Contour plots

ggplot(diamonds, aes(x = carat, price))+ 
  geom_point() + geom_density_2d() 

Interactive plots

plotly

g <- ggplot(mpg, aes(x = cty, y = hwy, colour = as.factor(cyl))) +
  geom_point(); ggplotly(g)

Super advanced plots

London population over time

https://github.com/Robinlovelace/Creating-maps-in-R

Spectroscopy graphs